import { login, logout, getInfo,loginMsm,nidayeHehe } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { allTenantlist } from "@/api/tenantApis/tenantManage";
import { getShopDetail } from '@/api/system/shopManage'
import {
  getPaihang
} from "@/api/tenantApis/indexStatistics.js";
import router from "@/router";
const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    userInfo:{},
    tenantList:[],//所有租户列表
    shopInfo:{},
    allYouzhiMap:{}
  },

  mutations: {
    SET_SHOPINFO: (state, shopInfo) => {
      state.shopInfo = shopInfo
    },
    SET_TENANTLIST: (state, tenantList) => {
      state.tenantList = tenantList
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_USERINFO:(state, userInfo) => {
      state.userInfo = userInfo
    },
    set_allYouzhiMap:(state,allYouzhiMap)=>{
      state.allYouzhiMap = {...allYouzhiMap}
    }
  },

  actions: {
    // 获取所有优质商家的列表
    getAllYouzhi({ state,commit }) {
      const obj = {}
      getPaihang().then((res) => {
        if(res.data&&res.data.length){
          res.data.forEach(r => {
            obj[r.tenantId] = {...r}

          });
        }
        commit('set_allYouzhiMap', obj)


      });
    },
    // 获取店铺信息
    getShopInfo({ state,commit },tenantId) {
      getShopDetail(tenantId||state.shopInfo.tenantId).then(res => {
        if (res.code == 200) {
          commit('SET_SHOPINFO', res.data)
        }
      });
    },
    // 获取供货商列表
    getAllTenantList({ commit }) {
      allTenantlist().then(res => {
        if (res.code == 200) {
          commit('SET_TENANTLIST', res.data)
        }
      });
    },
    // 登录 hehe
    nidayeHehe({ commit }, userInfo) {
      userInfo.username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        if(userInfo.loginType=='account'){
          nidayeHehe(userInfo).then(res => {
            setToken(res.token)
            commit('SET_TOKEN', res.token)
            resolve()
          }).catch(error => {
            reject(error)
          })
        }else{
          loginMsm(userInfo).then(res => {
            setToken(res.token)
            commit('SET_TOKEN', res.token)
            resolve()
          }).catch(error => {
            reject(error)
          })
        }

      })
    },

    // 登录
    Login({ commit }, userInfo) {
      userInfo.username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        if(userInfo.loginType=='account'){
          login(userInfo).then(res => {
            setToken(res.token)
            commit('SET_TOKEN', res.token)
            resolve()
          }).catch(error => {
            reject(error)
          })
        }else{
          loginMsm(userInfo).then(res => {
            setToken(res.token)
            commit('SET_TOKEN', res.token)
            resolve()
          }).catch(error => {
            reject(error)
          })
        }

      })
    },

    // 获取用户信息
    GetInfo({ commit,dispatch, state },flag=false) {
      dispatch('getAllYouzhi')
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.user
          const avatar = user.avatar;
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', user.userName)
          commit('SET_AVATAR', avatar)
          commit('SET_USERINFO', user)
          dispatch('getShopInfo',user.tenantId)
          if(!flag){
            dispatch('getAllTenantList')
          }
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
